//
//  WebBrowserSensor.m
//  Pennyworth
//
//  Created by Chris Karr on 12/24/07.
//  Copyright 2007 Chris J. Karr. All rights reserved.
//

#import "WebBrowserSensor.h"
#import "Context.h"
#import "SensorsController.h"

#define SENSOR_NAME @"Web Browser"

@implementation WebBrowserSensor

- (void) awakeFromNib
{
	[[SensorsController sharedController] addSensor:SENSOR_NAME];
	
	timer = [[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(update:) userInfo:nil repeats:YES] retain];
	[timer fire];
}

- (void) update:(NSTimer *) theTimer
{
	if (![[SensorsController sharedController] canFire:SENSOR_NAME])
	{
		NSMutableDictionary * note = [NSMutableDictionary dictionary];
		[note setValue:@"Current Safari Site" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

		note = [NSMutableDictionary dictionary];
		[note setValue:@"Current Firefox Site" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

		note = [NSMutableDictionary dictionary];
		[note setValue:@"Current Camino Site" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		
		return;
	}		
	
	BOOL safariOpen = NO;
	BOOL firefoxOpen = NO;
	BOOL caminoOpen = NO;
	
	NSWorkspace * workspace = [NSWorkspace sharedWorkspace];

	NSDictionary * active = [workspace activeApplication];

	if ([[active valueForKey:@"NSApplicationName"] isEqual:@"Safari"])
		safariOpen = YES;
	else if ([[active valueForKey:@"NSApplicationName"] isEqual:@"Firefox"])
		firefoxOpen = YES;
	else if ([[active valueForKey:@"NSApplicationName"] isEqual:@"Camino"])
		caminoOpen = YES;

	NSArray * apps = [workspace launchedApplications];
	
	int i = 0;
	for (i = 0; i < [apps count]; i++)
	{
		if ([[[apps objectAtIndex:i] valueForKey:@"NSApplicationName"] isEqual:@"Safari"])
			safariOpen = YES;
		else if ([[[apps objectAtIndex:i] valueForKey:@"NSApplicationName"] isEqual:@"Firefox"])
			firefoxOpen = YES;
		else if ([[[apps objectAtIndex:i] valueForKey:@"NSApplicationName"] isEqual:@"Camino"])
			caminoOpen = YES;
	}

	if (safariOpen)
	{
		NSAppleScript * script = [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\"\n\
																			if (exists document 1) then\n\
																				URL of document 1\n\
																			end if\n\
																		end tell"];

		NSAppleEventDescriptor * desc = [script executeAndReturnError:nil];
		
		if (desc != nil && [desc stringValue] != nil)
		{
			NSURL * url = [NSURL URLWithString:[desc stringValue]];
			
			NSString * host = [url host];
			
			if (host == nil)
				host = @"localhost";

			NSMutableDictionary * note = [NSMutableDictionary dictionary];
	
			[note setValue:@"Current Safari Site" forKey:OBSERVATION_SENSOR];
			[note setValue:host forKey:OBSERVATION_OBSERVATION];
			[note setValue:[NSNumber numberWithInteger:11] forKey:OBSERVATION_LIFESPAN];

			[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		}
		
		[script release];
	}

	if (firefoxOpen)
	{
		NSAppleScript * script = [[NSAppleScript alloc] initWithSource:@"tell application \"Firefox\"\n\
																			set myFirefox to properties of front window as list\n\
																			get item 3 of myFirefox\n\
																		end tell"];

		NSAppleEventDescriptor * desc = [script executeAndReturnError:nil];
		
		if (desc != nil && [desc stringValue] != nil)
		{
			NSURL * url = [NSURL URLWithString:[desc stringValue]];
			
			NSString * host = [url host];
			
			if (host == nil)
				host = @"localhost";

			NSMutableDictionary * note = [NSMutableDictionary dictionary];
	
			[note setValue:@"Current Firefox Site" forKey:OBSERVATION_SENSOR];
			[note setValue:host forKey:OBSERVATION_OBSERVATION];
			[note setValue:[NSNumber numberWithInteger:20] forKey:OBSERVATION_LIFESPAN];

			[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		}
		
		[script release];
	}

	if (caminoOpen)
	{
		NSAppleScript * script = [[NSAppleScript alloc] initWithSource:@"tell application \"Camino\"\n\
								  URL of the current tab of front window\n\
								  end tell"];
		
		NSAppleEventDescriptor * desc = [script executeAndReturnError:nil];
		
		if (desc != nil && [desc stringValue] != nil)
		{
			NSURL * url = [NSURL URLWithString:[desc stringValue]];
			
			NSString * host = [url host];
			
			if (host == nil)
				host = @"localhost";
			
			NSMutableDictionary * note = [NSMutableDictionary dictionary];
			
			[note setValue:@"Current Camino Site" forKey:OBSERVATION_SENSOR];
			[note setValue:host forKey:OBSERVATION_OBSERVATION];
			[note setValue:[NSNumber numberWithInteger:20] forKey:OBSERVATION_LIFESPAN];
			
			[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		}
	
		[script release];
	}
}

@end
